package com.linciping.useges.plugin.action;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import com.linciping.useges.plugin.asyc.ProjectResUsegesThread;
import com.linciping.useges.plugin.bean.File2R;
import com.linciping.useges.plugin.listener.ThreadFinishListener;
import com.linciping.useges.plugin.util.AndroidUtil;
import com.linciping.useges.plugin.util.Util;
import com.linciping.useges.plugin.view.BackgroundProgressWindow;
import org.jetbrains.android.dom.manifest.AndroidManifestXmlFile;

import java.util.ArrayList;
import java.util.List;

public class ProjectResUsegesAction extends ResUsegesAction {

    @Override
    protected void find(Project project, List<File2R> resRList, Module targetModule) {
        AndroidManifestXmlFile androidManifestXmlFile = AndroidUtil.getModuleAndroidManifestXmlFile(targetModule);
        if (androidManifestXmlFile == null) {
            return;
        }
        final String modulePackageName = androidManifestXmlFile.getPackageName();
        Module[] modules = ModuleManager.getInstance(project).getModules();
        List<Module> moduleList = new ArrayList<>();
        for (Module module : modules) {
            if (!module.equals(targetModule)) {
                moduleList.add(module);
            }
        }
        ProjectResUsegesThread projectResUsegesThread = new ProjectResUsegesThread(moduleList, project, targetModule, resRList, modulePackageName);
        ApplicationManager.getApplication().runWriteAction(projectResUsegesThread);
    }

    @Override
    protected boolean canUpdate(AnActionEvent e) {
        return Util.hasModule(e);
    }
}
